import {edits, findAllPower, findOnePower} from '@/api/power'
import store from "@/store";
import {powerSelect} from "@/api/user";
export default {
  name:'Power',
  data() {
    return {
      tableData: [],
      dialogVisible:false,
      disabled:false,
      form: {
        name: '',
        level:'',
        tip:'',
        id:''
      },
      formLabelWidth: '120px',
      dialogFormVisible: false,
    }
  },
  methods: {
    handleEdit(index, row) {

      if (store.getters.powerCheck.powerLevel == ""){
        this.$message({
          message: '权限状态过期，请重新登录获取管理权限状态',
          type: 'error'
        });
      }else if (store.getters.powerCheck.powerLevel == 100){
        this.editInfo(index , row)
      }else if (store.getters.powerCheck.powerLevel == 200){
        this.$message({
          message: '你所在的用户组禁止修改本界面',
          type: 'error'
        });
      }else if (store.getters.powerCheck.powerLevel == 300){
        this.$message({
          message: '你所在的用户组禁止修改本界面',
          type: 'error'
        });
      }else {
        this.$message({
          message: '权限非法，请联系管理员',
          type: 'error'
        });
      }
    },
    editPowerInfo(){
      let data = {
        pwName : this.form.name,
        pwTip:this.form.tip,
        pwId:this.form.id
      }
      edits(data).then(res =>{
        if (res.data >0){
          this.$message({
            message: '修改成功',
            type: 'success'
          });
        }
        this.findsAll()
        this.dialogFormVisible = false
      })
    },
    handleDelete(index, row) {

    },
    findsAll(){
      //权限校验
      //这里访问的是getters状态树
      if (store.getters.powerCheck.name == "") {
        this.$message({
          message: '权限状态过期，建议重新登录获取管理权限状态',
          type: 'error'
        });
      }else {
        this.powerChecks()
      }
      findAllPower().then(res =>{
        if (res.data != null){
          this.$message({
            type: 'success',
            message: '请求成功'
          });
          this.tableData = res.data
        }else {}
      })
    },
    editInfo(index ,row){
      this.dialogFormVisible = true
      let params = {
        pwId:row.pwId
      }
      findOnePower(params).then(res =>{
        this.form.name = res.data.pwName
        this.form.tip = res.data.pwTip
        this.form.level = res.data.pwLevel
        this.form.id = res.data.pwId
      })
    },
    powerChecks(){
      let data = {
        lgUsername: store.getters.powerCheck.name,
        lgPassword: store.getters.powerCheck.password
      }
      powerSelect(data).then(res => {
        store.dispatch("powerCheck/setPower", res.data.power.pwLevel);
      })
    },
  },
  created() {
    this.findsAll();
  },
}
